<?php
/**
 * Screen to create a new Mission
 * 
 * @author Xavier VILAIN
 * @package Hive\Mission
 * 
 */
use Fox\Html\Block;
use Hive\Mission\Partner;
use Fox\Generator;
use Fox\Html\Screen;
$xview = Xview::instance();

$screen = new Screen('HDR','create');
$screen->pageTitle(Msg(Hive::BUNDLE,Hive::MISSION_TITLE_CREATE));
$block = Xview::instance()->component('missions', 'header');
$block->id('createMission');
$block->item('status')->initialize(Hive\Mission::STATUS_OPEN);

$screen->add($block);

if($screen->submitted())
{
	$inputs = $screen->get();
	$grouping = $block->item('grouping');
	if(!empty($grouping)) {
		$inputs['grouping'] = !empty($inputs['grouping']) ? $inputs['grouping'] :
								 $block->item('grouping')->firstAvailableValue();
	}
	
	if(!$screen->errors()) {
		// Check if the contact already exist ?
		if(!empty($inputs['customerid']))
		{
			
		} else {
			// Contact Does not Exist 
			$url = Generator::url('ressource','add',null,array('name1' => $inputs['customer']));
			Fox\Message::Error(Msg(Hive::BUNDLE,
								Hive::CONTACT_DOES_NOT_EXIST_YET,
								$inputs['customer'],
								$url));
		}		
	}
	if(!$screen->errors())
	{
		try {
			// Check if the school already exist
			if(!empty($inputs['companyName']))
			{
				$company = Hive\Company::lookup($inputs['companyName']);
				if(!$company)
				{
					$company = new Hive\Company();
					$company->setFields(array('company_name' => $inputs['companyName']));
					$company->create();
				}
				$inputs['companid'] = $company->getId();
			}
			
			$mission = new Hive\Mission();
			$inputs['status'] = empty($inputs['status']) ? Hive\Mission::STATUS_OPEN : $inputs['status'];
			$mission->setFields($inputs);
			// Adding Mission Partners - Company
			$partner = new Partner();
			$partner->setFields(array('partnerid' => $inputs['companid'],
										'role' => Partner::ROLE_ACCOUNT,
										'status' => Partner::STATUS_DEFAULT));
			$mission->addToPartners($partner);
			// Adding Customer (Sold-To)
			$partner = new Partner();
			$partner->setFields(array('partnerid' => $inputs['customerid'],
										'role' => Partner::ROLE_SOLD_TO,
										'defaultflag' => 1,
										'status' => Partner::STATUS_DEFAULT));
			$mission->addToPartners($partner);
			// Add Role Responsible
			$partner = new Partner();
			$partner->setFields(array('partnerid' => $inputs['customerid'],
										'role' => Partner::ROLE_RESPONSIBLE,
										'defaultflag' => 1,
										'status' => Partner::STATUS_DEFAULT));
			$mission->addToPartners($partner);
			// Create
			$mission->create();
			Fox\Message::Success(Msg(Hive::BUNDLE,
										Hive::MISSION_SUCCESSFULLY_CREATED,
										$inputs['reference']));
			$id = $mission->getId();
			$screen->redirect(Generator::url('missions','main',null,array('missionid' => $id)));
		}
		catch(Fox\Object\Exception\Save $e)
		{
			Fox\Message::Error($e->getMessage());
			var_dump($e);
		}
	}
}
echo $screen->Display();